#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>

#define MYPORT 10001
#define MAXLINE 1024

int main(int argc, char** argv){
    int sock_fd;
    struct sockaddr_in seraddr;
    if(argc != 2){
        printf("usage: simple_client <IPaddress>\n");
        return 1;
    }

    sock_fd = socket(AF_INET, SOCK_STREAM, 0);
    memset(&seraddr, 0, sizeof(seraddr));
    seraddr.sin_family = AF_INET;
    seraddr.sin_port = htons(MYPORT);
    if(inet_pton(AF_INET, argv[1], &seraddr.sin_addr) < 0){
        perror("inet_pton()");
    }

    connect(sock_fd, (struct sockaddr*)&seraddr, sizeof(seraddr));
    char sendline[MAXLINE], recvline[MAXLINE];
    int n = 0;
    while(fgets(sendline, MAXLINE, stdin) != NULL){
        //要加上终止符
        write(sock_fd, sendline, strlen(sendline) + 1);
        sleep(2);
        n = read(sock_fd, recvline, MAXLINE);
        fputs(recvline, stdout);
        fflush(stdout);
    }
    return 0;
}